package org.casper.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.casper.bean.User;
import org.casper.service.UserService;
import org.casper.util.MD5Util;
import org.casper.util.SendMail;

/**
 * 用户控制器
 * project_name:forum<br>
 * package_name:org.casper.web<br>
 * file_name:UserServlet.java<br>
 * @author Casper<br>
 * create_date:2015年10月22日 上午10:11:49<br>
 * modified_date:
 * description:
 *
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserService();
	private HttpSession session = null;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		session = request.getSession();
		request.setCharacterEncoding("utf-8");  //设置请求的编码
		String op = request.getParameter("op");	//获取请求的操作
		
		if(op.equals("adminLogin")){			//管理员登录请求
			adminLogin(request,response);
		}else if(op.equals("userLogin")){		//用户登录请求
			userLogin(request,response);
		}else if(op.equals("userRegister")){
			userRegister(request,response);
		}else if(op.equals("userActive")){
			userActive(request,response);
		}
	}
	/**
	 * description:
	 * 2015年10月23日 下午4:41:26
	 * casper
	 * void
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void userActive(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String email = request.getParameter("email");
		String code = request.getParameter("code");
		if(userService.validateEmailExists(email)){
			if(code.equals(MD5Util.encodeStr(email))){
				User user = userService.userActive(email);
				if(user!=null){
					session.setAttribute("user", user);
					response.sendRedirect("UserIndexServlet");
				}
			}
		}
		
		
	}

	/**
	 * 处理用户注册的方法
	 * description:
	 * 2015年10月23日 下午2:58:20
	 * casper
	 * void
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void userRegister(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String code = request.getParameter("code");
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		String ip = request.getRemoteAddr();
		String sessionCode = (String) session.getAttribute("code");
		User regUser = new User();
		regUser.setEmail(email);
		regUser.setPass(MD5Util.encodeStr(pass));
		regUser.setLastip(ip);
		if(code.equals(sessionCode)){
			if(userService.addUser(regUser) == 1){
				/**
				 * 发邮件事件
				 * 
				 * 发件人
				 * 收件人  
				 * 主题
				 * 正文
				 * 附件
				 * 
				 * http://www.oracle.com/technetwork/java/javamail/index.html
				 * mail.jar  activation.jar
				 * 
				 * 邮件的协议
				 */
				String content = "http://localhost:8080/forum/UserServlet?op=userActive&email="+email+"&code="+MD5Util.encodeStr(email);
				new SendMail(email,content).start();
				session.setAttribute("tips", email+"请查看邮箱，速度激活");
				
				request.getRequestDispatcher("user-reg-active-email.jsp").forward(request, response);
			}else{
				session.setAttribute("tips", "异常");
				response.sendRedirect("user-register.jsp");
			}
		}else{
			session.setAttribute("tips", "验证码不正确");
			response.sendRedirect("user-register.jsp");
		}
		
		
	}

	/**
	 * 用户登录处理方法
	 * description:
	 * 2015年10月22日 上午10:21:15
	 * casper
	 * void
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void userLogin(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String email =request.getParameter("email");
		String pass = request.getParameter("pass");
		String ip = request.getRemoteAddr();
		User user = new User();
		user.setEmail(email);
		user.setPass(MD5Util.encodeStr(pass));
		user.setLastip(ip);
		User newUser= userService.userLogin(user);
		if(newUser!=null){
			session.setAttribute("user", newUser);
			response.sendRedirect("user-index.jsp");
		}

	}

	/**
	 * description:管理员登录处理方法
	 * 2015年10月22日 上午10:19:12
	 * casper
	 * void
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void adminLogin(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		String ip = request.getRemoteAddr();
		
		User user = new User();
		user.setEmail(email);
		user.setPass(MD5Util.encodeStr(pass));
		user.setLastip(ip);
		
		User newUser = userService.adminLogin(user);
		
		if(newUser!=null){
			session.setAttribute("user", newUser);
			if(request.getParameter("rememberme") != null){ 			//记住密码 也就是保存密码到客户端
				Cookie emailCookie = new Cookie("email", email);
				emailCookie.setMaxAge(3600*24*7);
				Cookie passCookie = new Cookie("pass", MD5Util.encodeStr(pass));
				passCookie.setMaxAge(3600*24*7);						//保存密码的时间设置为一周
				response.addCookie(emailCookie);
				response.addCookie(passCookie);
			}
			response.sendRedirect("BoardServlet?op=adminIndex");
		}else{
			session.setAttribute("tips", "用户名或者密码错误");
			response.sendRedirect("admin-login.jsp");
		}
		
	}
       
    
}
